<?php
 
class Personality_traits extends MY_Controller 
{
	var $curl = 'personality_traits';
	var $name = 'Personality Traits';
	var $search = '';
	var $language;
	var $submenu = "_menu";
	var $id_category;
	var $category_alias;

	function __construct()
	{
		parent::__construct();
        
        $this->load->model(array('/public/personality_traits_model', '/public/personality_traits_range_model'));
        
		$this->validation->set_fields($this->personality_traits_model->fields);

		$this->data['Title'] = T_('Personality Traits');

		$this->language = parent::getLang();
		
		$this->name = T_('Personality Traits');		
    }
	
	function index($id_test)
	{		
		$this->personality_traits_model->search(array('id_test' => $id_test));
		//$this->search = $this->data['search'];
		
		$this->renderLayout();
    }

	function create($id_test)
	{
		$this->data['id_test'] = $id_test;
		$this->data['points_from'] 			= $this->input->post('points_from');
		$this->data['points_to']   			= $this->input->post('points_to');
		$this->data['points_description']   = $this->input->post('points_description');
		if (empty($this->data['points_from']))
		{
			$this->data['points'] = array( '0' => array('from' => '', 'to' => '', 'description' => '') );
		}
		else
		{
			foreach ($this->data['points_from'] as $i => $point)
			{
				$from 			= $point;
				$to 			= $this->data['points_to'][$i];
				$description 	= $this->data['points_description'][$i];
				$this->data['points'][] = array('from' => $from, 'to' => $to, 'description' => $description);
			}
		}
		
        if (count($_POST))
		{
			$rules = array('trait', 'description');
			$this->validation->set_rules($this->personality_traits_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{			
				$item['id_tests'] 	= $id_test;				
				$item['trait'] 		= $this->validation->trait;
				$item['description']= $this->validation->description;

				if ($id_trait = $this->personality_traits_model->insert($item))
				{
					if (!empty($id_trait) && !empty($this->data['points_from']))
					{
						foreach ($this->data['points_from'] as $i => $point)
						{
							$item2 = array();
							$item2['id_tests_traits']= $id_trait;
							$item2['value_from']	= $point;
							$item2['value_to']		= $this->data['points_to'][$i];
							$item2['text'] 			= $this->data['points_description'][$i];
							
							$this->personality_traits_range_model->insert($item2);
						}
					}

					redirect($this->curl . '/index/'. $id_test);
				}				
			}
		}

		$this->data['Title'] .= ' / '. T_('New Trait');
		$this->data['wysiwyg'] = true;
		
		$this->renderLayout();        
    }
    
	function edit($id)
	{	
		if (!empty($id))
		{
			$object = $this->personality_traits_model->get($id);
			
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
				return false; 
			}
			
			$this->data['id_test'] 				= $object['id_tests'];
			$this->data['points_from'] 			= $this->input->post('points_from');
			$this->data['points_to']   			= $this->input->post('points_to');
			$this->data['points_description']   = $this->input->post('points_description');
			
		
			if (!count($_POST))
			{
				$points = $this->personality_traits_range_model->search(array('id_trait' => $id));
				
				$this->validation->id 		= $object['id']; 
				$this->validation->id_test	= $object['id_tests']; 
				$this->validation->trait 	= $object['trait'];
				$this->validation->description 	= $object['description'];	

				if (empty($points))
				{
					$this->data['points'] = array( '0' => array('id' => '0', 'value_from' => '', 'value_to' => '', 'text' => '') );
				}
				else
				{
					$this->data['points'] = $points;
				}					
				//pr($this->data['points']);		
			}
			else
			{
				$rules = array('trait', 'description');
				$this->validation->set_rules($this->personality_traits_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 			= $this->validation->id;
					$item['trait'] 			= $this->validation->trait;
					$item['description'] 	= $this->validation->description;
					
					$this->personality_traits_model->update($item);					
					
					/*Traits Range*/
					$this->data['points_from'] 			= $this->input->post('points_from');
					$this->data['points_to']   			= $this->input->post('points_to');
					$this->data['points_description']   = $this->input->post('points_description');
					
					$res = $this->db->delete('tests_traits_range', array('id_tests_traits' => $id));
					
					if (!empty($this->data['points_from']))
					{
						foreach ($this->data['points_from'] as $i => $point)
						{
							
							$item2 = array();
							$item2['id_tests_traits']= $id;
							$item2['value_from']	= $point;
							$item2['value_to']		= $this->data['points_to'][$i];
							$item2['text'] 			= $this->data['points_description'][$i];
							
							if ($item2['value_to'] > 0)
							{
								$this->personality_traits_range_model->insert($item2);	
							}							
						}
					}
					
					redirect($this->curl . '/index/' . $this->input->post('id_test'));				
				}
			}			
		}
           
		$this->data['Title'] .= ' / ' . $object['trait'] . ' / '. T_('Edit');
		$this->data['wysiwyg'] = true; 
		
		$this->renderLayout();
    }
        
	function delete($id, $id_test)
	{
		$data = $this->personality_traits_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index/'. $id_test);	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}
	        
	function deleteItems()
	{
		$data = $this->personality_traits_model->delete($_REQUEST['items']);

		echo $data;
	}
	    
  function deleteRange($id)
  {
    echo $this->personality_traits_range_model->delete($id);
  }
  
	function activity()
	{
		$data = $this->utils->publishItems($this->article_model->table, $this->article_model->prKey, $this->article_model->publishedField, $_REQUEST['items'], $_REQUEST['action']);
		echo $this->utils->php2js($data);
	}
	
	/*
	 * Static Pages 
	*/
	function view($id)
	{
		$this->data['Title'] = '';
		
		$this->data['notification'] = $this->personality_traits_model->get($id);
		
		if (!empty($this->data['notification']))
		{
			$this->data['Title'] = $this->data['notification']->title;
		}
		
		$this->view = '/notifications/view';
		
		$this->renderLayout();	
	}
}

?>